package com.bawei.module_mine.ui.tab

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.bawei.module_mine.R
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_mine.http.viewModel.LoginViewModel
import com.bawei.module_mine.http.state.LoginState
import com.bawei.module_mine.model.LikeModelItem
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import android.widget.ImageView
import android.widget.TextView
import com.bawei.lib_storage.mmkv.AuthMMKV
import com.bawei.module_mine.http.intent.LoginIntent
import com.bumptech.glide.Glide

class LikeFragment : Fragment() {
    private lateinit var viewModel: LoginViewModel
    private lateinit var adapter: LikeAdapter

    override fun onCreateView(
        inflater: LayoutInflater, 
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_like, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val recyclerView = view.findViewById<RecyclerView>(R.id.rv_like_list)
        adapter = LikeAdapter()
        recyclerView.layoutManager = LinearLayoutManager(requireContext())
        recyclerView.adapter = adapter

        viewModel = ViewModelProvider(this)[LoginViewModel::class.java]
        observeLikeList()
        val id = AuthMMKV.uId
        lifecycleScope.launch {
            viewModel.intents.send(LoginIntent.Like("0", id))
        }
    }

    private fun observeLikeList() {
        viewLifecycleOwner.lifecycleScope.launch {
            viewModel.state.collectLatest { state ->
                when (state) {
                    is LoginState.LikeSuccess -> {
                        adapter.submitList(state.data)
                    }
                    is LoginState.Error -> {
                        Toast.makeText(requireContext(), state.error, Toast.LENGTH_SHORT).show()
                    }
                    else -> {}
                }
            }
        }
    }
    
    private fun initLikeData() {
        // 初始化喜欢数据
        // 这里可以加载用户喜欢的内容列表
    }
}

// Adapter for LikeModelItem
class LikeAdapter : RecyclerView.Adapter<LikeAdapter.LikeViewHolder>() {
    private var items: List<LikeModelItem> = emptyList()
    fun submitList(list: List<LikeModelItem>) {
        items = list
        notifyDataSetChanged()
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LikeViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_like, parent, false)
        return LikeViewHolder(view)
    }
    override fun onBindViewHolder(holder: LikeViewHolder, position: Int) {
        holder.bind(items[position])
    }
    override fun getItemCount(): Int = items.size
    class LikeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val ivPreview: ImageView = itemView.findViewById(R.id.iv_preview)
        private val tvTitle: TextView = itemView.findViewById(R.id.tv_title)
        private val tvPublishTime: TextView = itemView.findViewById(R.id.tv_publish_time)
        fun bind(item: LikeModelItem) {
            tvTitle.text = item.title
            tvPublishTime.text = item.publish_time?.toString()
            Glide.with(itemView).load(item.videomainimag).into(ivPreview)
        }
    }
}